| Server IP : 121.121.20.254 / Your IP : 216.73.216.202 Web Server : Microsoft-IIS/10.0 System : Windows NT WEB-SERVER 10.0 build 20348 (Windows Server 2022) AMD64 User : IUSR ( 0) PHP Version : 8.3.28 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : C:/Program Files/LibreOffice/help/en-US/text/sbasic/shared/ |
Upload File : |
<!DOCTYPE html>
<html lang="en-US" dir="ltr">
<head>
<base href="../../../../">
<noscript><meta http-equiv="refresh" content="0; URL=../../../../en-US/noscript.html"></noscript>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Using Variables</title>
<link rel="shortcut icon" href="media/navigation/favicon.ico">
<link type="text/css" href="normalize.css" rel="Stylesheet">
<link type="text/css" href="prism.css" rel="Stylesheet">
<link type="text/css" href="default.css" rel="Stylesheet">
<script type="text/javascript" src="polyfills.js"></script><script type="text/javascript" src="languages.js"></script><script type="text/javascript" src="en-US/langnames.js"></script><script type="text/javascript" src="flexsearch.debug.js"></script><script type="text/javascript" src="prism.js"></script><script type="text/javascript" src="help2.js" defer></script><script type="text/javascript" src="a11y-toggle.js" defer></script><script type="text/javascript" src="paginathing.js" defer></script><script type="text/javascript" src="en-US/bookmarks.js" defer></script><script type="text/javascript" src="en-US/contents.js" defer></script><script type="text/javascript" src="help.js" defer></script><meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<body>
<header id="TopLeftHeader"><a class="symbol" href="en-US/text/shared/05/new_help.html"><div></div></a><a class="logo" href="en-US/text/shared/05/new_help.html"><p dir="auto">LibreOffice 24.2 Help</p></a><div class="dropdowns"><div class="modules">
<button type="button" data-a11y-toggle="modules-nav" id="modules" aria-haspopup="true" aria-expanded="false" aria-controls="modules-nav">Module</button><nav id="modules-nav" hidden=""></nav>
</div></div></header><aside class="leftside"><input id="accordion-1" name="accordion-menu" type="checkbox"><label for="accordion-1" dir="auto">Contents</label><div id="Contents" class="contents-treeview"></div></aside><div id="SearchFrame"><div id="Bookmarks">
<input id="search-bar" type="search" class="search" placeholder="Search in bookmarks for chosen module" dir="auto"><div class="nav-container" tabindex="0"><nav class="index" dir="auto"></nav></div>
</div></div>
<div id="DisplayArea" itemprop="softwareHelp" itemscope="true" itemtype="http://schema.org/SoftwareApplication">
<a name="variable"></a>
<a name="bm_id3149346"></a>
<meta itemprop="keywords" content="names of variables">
<meta itemprop="keywords" content="variables, using">
<meta itemprop="keywords" content="types of variables">
<meta itemprop="keywords" content="declaring variables">
<meta itemprop="keywords" content="values,of variables">
<meta itemprop="keywords" content="literals,date">
<meta itemprop="keywords" content="literals,integer">
<meta itemprop="keywords" content="literals,floating point">
<meta itemprop="keywords" content="constants">
<meta itemprop="keywords" content="arrays,declaring">
<meta itemprop="keywords" content="defining,constants">
<h1 id="hd_id3149346" dir="auto">Using Variables</h1>
<p id="par_id3154346" class="paragraph" dir="auto">The following describes the basic use of variables in LibreOffice Basic.</p>
<h2 id="hd_id3153361" dir="auto">Naming Conventions for Variable Identifiers</h2>
<p id="par_id3148797" class="paragraph" dir="auto">A variable name can consist of a maximum of 255 characters. The first character of a variable name <span class="emph">must</span> be a letter A-Z or a-z. Numbers can also be used in a variable name, but punctuation symbols and special characters are not permitted, with exception of the underscore character ("_"). In LibreOffice Basic variable identifiers are not case-sensitive. Variable names may contain spaces but must be enclosed in square brackets if they do.</p>
<p id="par_id3156422" class="paragraph" dir="auto">Examples for variable identifiers:</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
MyNumber=5 'Correct'
MyNumber5=15 'Correct'
MyNumber_5=20 'Correct'
My Number=20 'Not valid, variable with space must be enclosed in square brackets'
[My Number]=12 'Correct'
DéjàVu=25 'Not valid, special characters are not allowed'
5MyNumber=12 'Not valid, variable may not begin with a number'
Number,Mine=12 'Not valid, punctuation marks are not allowed'
</code></pre></div>
<h2 id="hd_id3146317" dir="auto">Declaring Variables</h2>
<p id="par_id3150299" class="paragraph" dir="auto">In LibreOffice Basic you don't need to declare variables explicitly. A variable declaration can be performed with the <span class="emph">Dim</span> statement. You can declare more than one variable at a time by separating the names with a comma. To define the variable type, use either a type-declaration sign after the name, or the appropriate key word. </p>
<p id="par_id3154118" class="paragraph" dir="auto">Examples for variable declarations:</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Dim a$ 'Declares the variable "a" as a String'
Dim a As String 'Declares the variable "a" as a String'
Dim a$, b As Integer 'Declares one variable as a String and one as an Integer'
Dim c As Boolean 'Declares c as a Boolean variable that can be TRUE or FALSE'
</code></pre></div>
<div class="warning">
<div class="noteicon" dir="auto"><img src="media/icon-themes/res/helpimg/warning.svg" alt="warning" style="width:40px;height:40px;"></div>
<div class="notetext"><p id="par_id3144770" dir="auto">Once you have declared a variable as a certain type, you cannot declare the variable under the same name again as a different type!</p></div>
</div>
<br>
<p id="par_id421619551219763" class="paragraph" dir="auto">When you declare multiple variables in a single line of code you need to specify the type of each variable. If the type of a variable is not explicitly specified, then Basic will assume that the variable is of the <span class="emph">Variant</span> type.</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
' Both variables "a" and "b" are of the Integer type
Dim a As Integer, b As Integer
' Variable "c" is a Variant and "d" is an Integer
Dim c, d As Integer
' A variable can also be explicitly declared as a Variant
Dim e As Variant, f As Double
</code></pre></div>
<div class="note">
<div class="noteicon" dir="auto"><img src="media/icon-themes/res/helpimg/note.svg" alt="note" style="width:40px;height:40px;"></div>
<div class="notetext"><p id="par_id521619551687371" dir="auto">The <span class="emph">Variant</span> type is a special data type that can store any kind of value. To learn more, refer to the section <a target="_top" href="en-US/text/sbasic/shared/01020100.html#VariantTypeH2">The Variant type</a> below.</p></div>
</div>
<br>
<h3 id="hd_id3149331" dir="auto">Forcing Variable Declarations</h3>
<p id="par_id3149443" class="paragraph" dir="auto">To force declaration of variables, use the following command:</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Option Explicit
</code></pre></div>
<p id="par_id3155072" class="paragraph" dir="auto">The <span class="emph">Option Explicit</span> statement has to be the first line in the module, before the first SUB. Generally, only arrays need to be declared explicitly. All other variables are declared according to the type-declaration character, or - if omitted - as the default type <span class="emph">Single</span>.</p>
<h2 id="hd_id3154614" dir="auto">Variable Types</h2>
<p id="par_id3155383" class="paragraph" dir="auto">LibreOffice Basic supports four variable classes:</p>
<ul itemprop="Unordered" itemscope="true" itemtype="http://schema.org/ItemList" dir="auto">
<li itemprop="itemListElement" itemscope="true" itemtype="http://schema.org/ItemListUnordered" dir="auto">
<p id="par_id3153972" class="listitem" dir="auto"> <span class="emph">Numeric</span> variables can contain number values. Some variables are used to store large or small numbers, and others are used for floating-point or fractional numbers. </p>
</li>
<li itemprop="itemListElement" itemscope="true" itemtype="http://schema.org/ItemListUnordered" dir="auto">
<p id="par_id3159226" class="listitem" dir="auto"> <span class="emph">String</span> variables contain character strings.</p>
</li>
<li itemprop="itemListElement" itemscope="true" itemtype="http://schema.org/ItemListUnordered" dir="auto">
<p id="par_id3145217" class="listitem" dir="auto"> <span class="emph">Boolean</span> variables contain either the TRUE or the FALSE value.</p>
</li>
<li itemprop="itemListElement" itemscope="true" itemtype="http://schema.org/ItemListUnordered" dir="auto">
<p id="par_id3154762" class="listitem" dir="auto"> <span class="emph">Object</span> variables can store objects of various types, like tables and documents within a document.</p>
</li>
</ul>
<h3 id="hd_id3153805" dir="auto">Integer Variables</h3>
<p id="par_id3146966" class="paragraph" dir="auto">Integer variables range from -32768 to 32767. If you assign a floating-point value to an integer variable, the decimal places are rounded to the next integer. Integer variables are rapidly calculated in procedures and are suitable for counter variables in loops. An integer variable only requires two bytes of memory. "%" is the type-declaration character.</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Dim Variable%
Dim Variable As Integer
</code></pre></div>
<h3 id="hd_id3147546" dir="auto">Long Integer Variables</h3>
<p id="par_id3151193" class="paragraph" dir="auto">Long integer variables range from -2147483648 to 2147483647. If you assign a floating-point value to a long integer variable, the decimal places are rounded to the next integer. Long integer variables are rapidly calculated in procedures and are suitable for counter variables in loops for large values. A long integer variable requires four bytes of memory. "&" is the type-declaration character.</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Dim Variable&
Dim Variable As Long
</code></pre></div>
<h3 id="hd_id7596972" dir="auto">Decimal Variables</h3>
<p id="par_id2649311" class="paragraph" dir="auto">Decimal variables can take positive or negative numbers or zero. Accuracy is up to 29 digits.</p>
<p id="par_id7617114" class="paragraph" dir="auto">You can use plus (+) or minus (-) signs as prefixes for decimal numbers (with or without spaces).</p>
<p id="par_id1593676" class="paragraph" dir="auto">If a decimal number is assigned to an integer variable, LibreOffice Basic rounds the figure up or down.</p>
<h3 id="hd_id3147500" dir="auto">Single Variables</h3>
<p id="par_id3153070" class="paragraph" dir="auto">Single variables can take positive or negative values ranging from 3.402823 x 10E38 to 1.401298 x 10E-45. Single variables are floating-point variables, in which the decimal precision decreases as the non-decimal part of the number increases. Single variables are suitable for mathematical calculations of average precision. Calculations require more time than for Integer variables, but are faster than calculations with Double variables. A Single variable requires 4 bytes of memory. The type-declaration character is "!".</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Dim Variable!
Dim Variable As Single
</code></pre></div>
<h3 id="hd_id3155753" dir="auto">Double Variables</h3>
<p id="par_id3150953" class="paragraph" dir="auto">Double variables can take positive or negative values ranging from 1.79769313486232 x 10E308 to 4.94065645841247 x 10E-324. Double variables are floating-point variables, in which the decimal precision decreases as the non-decimal part of the number increases. Double variables are suitable for precise calculations. Calculations require more time than for Single variables. A Double variable requires 8 bytes of memory. The type-declaration character is "#".</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Dim Variable#
Dim Variable As Double
</code></pre></div>
<h3 id="hd_id3155747" dir="auto">Currency Variables</h3>
<p id="par_id3153337" class="paragraph" dir="auto">Currency variables are internally stored as 64-bit numbers (8 Bytes) and displayed as a fixed-decimal number with 15 non-decimal and 4 decimal places. The values range from -922337203685477.5808 to +922337203685477.5807. Currency variables are used to calculate currency values with a high precision. The type-declaration character is "@".</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Dim Variable@
Dim Variable As Currency
</code></pre></div>
<h3 id="hd_id301576839713868" dir="auto">Literals for integers</h3>
<p id="par_id1001576839723156" class="paragraph" dir="auto">Numbers can be encoded using octal and hexadecimal forms.</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
xi = &o13 ' 8 + 3
ci = &h65 ' 6*16 + 5
MAX_Integer = &o77777 ' 32767 = &h7FFF
MIN_Integer = &o100000 ' -32768 = &h8000
MAX_Long = &h7fffffff ' 2147483647 = &o17777777777
MIN_Long = &h80000000 ' -2147483648 = &o20000000000
</code></pre></div>
<h3 id="hd_id3148742" dir="auto">String Variables</h3>
<p id="par_id3151393" class="paragraph" dir="auto">String variables can hold character strings with up to 2,147,483,648 characters. Each character is stored as the corresponding Unicode value. String variables are suitable for word processing within programs and for temporary storage of any non-printable character up to a maximum length of 2 Gbytes. The memory required for storing string variables depends on the number of characters in the variable. The type-declaration character is "$".</p>
<div class="tip">
<div class="noteicon" dir="auto"><img src="media/icon-themes/res/helpimg/tip.svg" alt="tip" style="width:40px;height:40px;"></div>
<div class="notetext"><p id="par_id381599081637549" dir="auto">In BASIC String functions, the first character of the string has index 1.</p></div>
</div>
<br>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Dim Variable$
Dim Variable As String
</code></pre></div>
<h3 id="hd_id3150534" dir="auto">Boolean Variables</h3>
<p id="par_id3145632" class="paragraph" dir="auto">Boolean variables store only one of two values: TRUE or FALSE. A number 0 evaluates to FALSE, every other value evaluates to TRUE.</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Dim Variable As Boolean
</code></pre></div>
<h3 id="hd_id3149722" dir="auto">Date Variables</h3>
<p id="par_id3159116" class="paragraph" dir="auto">Date variables can only contain dates and time values stored in an internal format. Values assigned to Date variables with <a target="_top" href="en-US/text/sbasic/shared/03030101.html"><span class="emph">Dateserial</span></a>, <a target="_top" href="en-US/text/sbasic/shared/03030102.html"><span class="emph">Datevalue</span></a>, <a target="_top" href="en-US/text/sbasic/shared/03030205.html"><span class="emph">Timeserial</span></a> or <a target="_top" href="en-US/text/sbasic/shared/03030206.html"><span class="emph">Timevalue</span></a> are automatically converted to the internal format. Date-variables are converted to normal numbers by using the <a target="_top" href="en-US/text/sbasic/shared/03030103.html"><span class="emph">Day</span></a>, <a target="_top" href="en-US/text/sbasic/shared/03030104.html"><span class="emph">Month</span></a>, <a target="_top" href="en-US/text/sbasic/shared/03030106.html"><span class="emph">Year</span></a> or the <a target="_top" href="en-US/text/sbasic/shared/03030201.html"><span class="emph">Hour</span></a>, <a target="_top" href="en-US/text/sbasic/shared/03030202.html"><span class="emph">Minute</span></a>, <a target="_top" href="en-US/text/sbasic/shared/03030204.html"><span class="emph">Second</span></a> function. The internal format enables a comparison of date/time values by calculating the difference between two numbers. These variables can only be declared with the key word <span class="emph">Date</span>.</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Dim Variable As Date
</code></pre></div>
<a name="bm_id3150669"></a>
<meta itemprop="keywords" content="ampersand symbol, in literal notation">
<meta itemprop="keywords" content="literals,hexadecimal">
<meta itemprop="keywords" content="literals,octal">
<meta itemprop="keywords" content="literals,&h notation">
<meta itemprop="keywords" content="literals,&o notation">
<h3 id="hd_DateLiterals" dir="auto">Literals for Dates</h3>
<a name="DateLiterals"></a>
<p id="par_id151616083357363" class="paragraph" dir="auto">Date literals allow to specify unambiguous date variables that are independent from the current language. Literals are enclosed between hash signs <span class="literal">#</span>. Possible formats are:</p>
<ul itemprop="Unordered" itemscope="true" itemtype="http://schema.org/ItemList" dir="auto">
<li itemprop="itemListElement" itemscope="true" itemtype="http://schema.org/ItemListUnordered" dir="auto">
<p id="par_id41616083766108" class="listitem" dir="auto"><span class="literal">#yyyy-mm-dd#</span></p>
</li>
<li itemprop="itemListElement" itemscope="true" itemtype="http://schema.org/ItemListUnordered" dir="auto">
<p id="par_id271616083874773" class="listitem" dir="auto"><span class="literal">#mm/dd/yyyy#</span></p>
</li>
</ul>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
start_date = #12/30/1899# ' = 1
dob = #2010-09-28#
</code></pre></div>
<a name="bm_id601619552599885"></a>
<meta itemprop="keywords" content="The Variant type">
<meta itemprop="keywords" content="The Any type">
<h2 id="VariantTypeH2" dir="auto">The Variant type</h2>
<p id="par_id1001619552129323" class="paragraph" dir="auto">Variables declared as <span class="emph">Variant</span> can handle any data type. This means that the actual data type is defined during runtime as a value is assigned to the variable.</p>
<p id="par_id631619552417188" class="paragraph" dir="auto">There are three main ways to create a <span class="emph">Variant</span> variable, as shown below:</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Dim varA ' The type is not specified, hence the variable is a Variant
Dim varB as Variant ' The variable is explicitly declared as a Variant
varC = "abc" ' Previously undeclared variables are treated as Variants
</code></pre></div>
<p id="par_id631619552417343" class="paragraph" dir="auto">The example below uses the <a target="_top" href="en-US/text/sbasic/shared/03103600.html">TypeName function</a> to show how the type of a <span class="emph">Variant</span> variable changes upon assignment.</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Dim myVar As Variant
MsgBox TypeName(myVar) ' Empty
myVar = "Hello!"
MsgBox TypeName(myVar) ' String
myVar = 10
MsgBox TypeName(myVar) ' Integer
</code></pre></div>
<div class="note">
<div class="noteicon" dir="auto"><img src="media/icon-themes/res/helpimg/note.svg" alt="note" style="width:40px;height:40px;"></div>
<div class="notetext"><p id="par_id141619553442668" dir="auto">A <span class="emph">Variant</span> variable is initialized with the <a target="_top" href="en-US/text/sbasic/shared/03040000.html#objectconstants">Empty</a> special data type. You can use the <a target="_top" href="en-US/text/sbasic/shared/03102400.html">IsEmpty function</a> to test if a variable is an <span class="emph">Empty Variant</span>.</p></div>
</div>
<br>
<p id="par_id541619552755706" class="paragraph" dir="auto">You can also use the keyword <span class="emph">Any</span> to declare a variable as a <span class="emph">Variant</span>. However, <span class="emph">Any</span> is deprecated and is available for backward compatibility.</p>
<div class="warning">
<div class="noteicon" dir="auto"><img src="media/icon-themes/res/helpimg/warning.svg" alt="warning" style="width:40px;height:40px;"></div>
<div class="notetext"><p id="par_id711619622934043" dir="auto">Arguments with type <span class="emph">Variant</span> or <span class="emph">Any</span> passed in function calls are not checked for their types.</p></div>
</div>
<br>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Dim myVar As Any ' Variable "myVar" is a Variant
</code></pre></div>
<h2 id="hd_id3148732" dir="auto">Initial Variable Values</h2>
<p id="par_id3154549" class="paragraph" dir="auto">As soon as the variable has been declared, it is automatically set to the "Null" value. Note the following conventions:</p>
<p id="par_id3143222" class="paragraph" dir="auto"> <span class="emph">Numeric</span> variables are automatically assigned the value "0" as soon as they are declared.</p>
<p id="par_id3150693" class="paragraph" dir="auto"> <span class="emph">Date variables</span> are assigned the value 0 internally; equivalent to converting the value to "0" with the <a target="_top" href="en-US/text/sbasic/shared/03030103.html"><span class="emph">Day</span></a>, <a target="_top" href="en-US/text/sbasic/shared/03030104.html"><span class="emph">Month</span></a>, <a target="_top" href="en-US/text/sbasic/shared/03030106.html"><span class="emph">Year</span></a> or the <a target="_top" href="en-US/text/sbasic/shared/03030201.html"><span class="emph">Hour</span></a>, <a target="_top" href="en-US/text/sbasic/shared/03030202.html"><span class="emph">Minute</span></a>, <a target="_top" href="en-US/text/sbasic/shared/03030204.html"><span class="emph">Second</span></a> function.</p>
<p id="par_id3154807" class="paragraph" dir="auto"> <span class="emph">String variables</span> are assigned an empty-string ("") when they are declared.</p>
<h2 id="hd_id3153936" dir="auto">Arrays</h2>
<p id="par_id3148736" class="paragraph" dir="auto">LibreOffice Basic knows one- or multi-dimensional arrays, defined by a specified variable type. Arrays are suitable for editing lists and tables in programs. Individual elements of an array can be addressed through a numeric index.</p>
<p id="par_id3149546" class="paragraph" dir="auto">Arrays <span class="emph">must</span> be declared with the <span class="emph">Dim</span> statement. There are several ways to define the index range of an array:</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Dim Text$(20) '21 elements numbered from 0 to 20'
Dim Text$(5,4) '30 elements (a matrix of 6 x 5 elements)'
Dim Text$(5 To 25) '21 elements numbered from 5 to 25'
Dim Text$(-15 To 5) '21 elements (including 0), numbered from -15 to 5'
</code></pre></div>
<p id="par_id3153005" class="paragraph" dir="auto">The index range can include positive as well as negative numbers. </p>
<h2 id="hd_id3154507" dir="auto">Constants</h2>
<p id="par_id3156357" class="paragraph" dir="auto">Constants have a fixed value. They are only defined once in the program and cannot be redefined later:</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Const ConstName=Expression
</code></pre></div>
<a name="relatedtopics"></a><div class="relatedtopics">
<p class="related" itemprop="mentions" dir="auto"><a name="related"></a><span class="emph">Related Topics</span>
</p>
<div class="relatedbody" itemprop="mentions">
<p id="par_id281619620439154" class="paragraph" dir="auto"><a target="_top" href="en-US/text/sbasic/shared/03040000.html">Basic Constants</a></p>
<p id="par_id641619620637722" class="paragraph" dir="auto"><a target="_top" href="en-US/text/sbasic/shared/03102100.html">Dim Statement</a></p>
</div>
</div>
</div>
<div id="DonationFrame"></div>
<footer><div id="DEBUG" class="debug">
<h3 class="bug">Help content debug info:</h3>
<p dir="auto">This page is: <a href="https://opengrok.libreoffice.org/xref/help/source/text/sbasic/shared/01020100.xhp" target="_blank">/text/sbasic/shared/01020100.xhp</a></p>
<p dir="auto">Title is: Using Variables</p>
<p id="bm_module" dir="auto"></p>
<p id="bm_system" dir="auto"></p>
<p id="bm_HID" dir="auto"></p>
</div></footer>
</body>
</html>